package struts;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import pojo.Difficulty;
import base.JsonPacker;
import base.JsonUnpacker;

import com.opensymphony.xwork2.ActionSupport;

import service.HomeworkService;
import service.QuestionGenerator;
import service.UserService;

public class HomeworkAction extends ActionSupport {

	private HomeworkService homeworkService;
	private QuestionGenerator questionGenerator;
	
	public void checkFinishedHomeworkList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String stuId = request.getParameter("id");
		write(ServletActionContext.getResponse(), homeworkService.checkFinishedHomeworkList(stuId)+"");
		
	}
	
	public void checkUnfinishedHomeworkList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String stuId = request.getParameter("id");
		write(ServletActionContext.getResponse(), homeworkService.checkUnfinishedHomeworkList(stuId)+"");
	}
	
	public void checkFinshedDetail(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String stuId = request.getParameter("studentId");
		String homId = request.getParameter("homeworkId");
		write(ServletActionContext.getResponse(), homeworkService.checkFinshedDetail(stuId,homId)+"");
	}
	
	public void getHomeworkDetail(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String homId = request.getParameter("homeworkId");
		write(ServletActionContext.getResponse(), homeworkService.getHomeworkDetail(homId)+"");
	}
	
	public void getQuestions(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String strDifficulties = request.getParameter("difficulty");
		String strNum = request.getParameter("number");
		try {
			write(ServletActionContext.getResponse(), 
					new JsonPacker().toJsonArray(questionGenerator.generateQuestions(
							Difficulty.formatToDifficulties(strDifficulties), Integer.parseInt(strNum))));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	public void doHomework(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String strStudentId = request.getParameter("studentId");
		String strHomeworkId = request.getParameter("homeworkId");
		String wrongAnswers = request.getParameter("wrongAnswers");
		try {
			write(ServletActionContext.getResponse(), 
					new JsonPacker().toJsonArray(questionGenerator.generateQuestions(
							Difficulty.formatToDifficulties(strDifficulties), Integer.parseInt(strNum))));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void write(HttpServletResponse response,String result){
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public HomeworkService getHomeworkService() {
		return homeworkService;
	}

	public void setHomeworkService(HomeworkService homeworkService) {
		this.homeworkService = homeworkService;
	}

	public QuestionGenerator getQuestionGenerator() {
		return questionGenerator;
	}

	public void setQuestionGenerator(QuestionGenerator questionGenerator) {
		this.questionGenerator = questionGenerator;
	}
	
}
